package com.livepan.server.utils;

import com.alibaba.fastjson.JSONObject;
import com.livepan.server.service.model.ProblemModel;
import com.livepan.server.service.model.SubmissionModel;
import org.apache.ibatis.io.Resources;
import redis.clients.jedis.Jedis;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Properties;

/**
 * 推送数据给判题端进行判题
 */
public class PushJudgerUtil {

    private InetAddress host;
    private int port;
    private byte[] message;
    private String problemChannel;
    private String contestChannel;

    private static PushJudgerUtil pushJudgerUtil;

    public static PushJudgerUtil getInstance() {
        if (pushJudgerUtil == null) {
            synchronized (PushJudgerUtil.class) {
                if (pushJudgerUtil == null) {
                    pushJudgerUtil = new PushJudgerUtil();
                }
            }
        }
        return pushJudgerUtil;
    }


    private PushJudgerUtil() {
        String resource = "judge.properties";
        Properties properties = new Properties();
        InputStream inputStream;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            properties.load(inputStream);
            host = InetAddress.getByName(properties.getProperty("host"));
            port = Integer.parseInt(properties.getProperty("port"));
            //todo 不晓得啥用
            message = ("1").getBytes();
            problemChannel = properties.getProperty("problemChannel");
            contestChannel = properties.getProperty("contestChannel");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public synchronized void pushJudge(@NotNull SubmissionModel submissionModel,
                                       @NotNull ProblemModel problemModel) throws IOException {
        Jedis jedis = null;
        DatagramSocket socket = null;
        DatagramPacket packet;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("contestId", submissionModel.getContestId());
        jsonObject.put("isSpj", problemModel.getSpj());
        jsonObject.put("language", submissionModel.getLanguage());
        jsonObject.put("memoryLimit", problemModel.getMemoryLimit()*1024);
        jsonObject.put("problemId", submissionModel.getProblemId());
        jsonObject.put("sourcecode", submissionModel.getSourcecode());
        jsonObject.put("submissionId", submissionModel.getRunId());
        jsonObject.put("timeLimit", problemModel.getTimeLimit()*1000);

        String request = jsonObject.toJSONString();
        try {
            jedis = GetJedisSession.getJedisSession();

            jedis.rpush(problemChannel, request);

            socket = new DatagramSocket();
            packet = new DatagramPacket(message, message.length, host, port);
            socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (socket != null) {
                socket.close();
            }
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
